package home.ybubnov.bugcrss;

import home.ybubnov.bugcrss.entity.WebPage;

import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;

public class SubsribeMailHandlerServlet extends MailHandlerServlet {

	@Override
	void processAddresses(Set<String> addresses) {
		EntityManager em = EMF.emf.createEntityManager();
		try {
			em.getTransaction().begin();
			WebPage page = null;
			List<WebPage> results = (List<WebPage>) em.createNamedQuery("WebPage.getWebPage").getResultList();
			if (results.size() > 0) {
				page = results.get(0);
			}
			if (page == null) {
				page = new WebPage();
			}
			page.addAddreses(addresses);

			em.persist(page);
			em.getTransaction().commit();
		} catch (Exception e) {
			if (em.getTransaction().isActive()) {
				em.getTransaction().rollback();
			}
			throw new RuntimeException(e);
		} finally {
			em.close();
		}
	}

}
